
var multiply = function(num1: string, num2: string): string {

    function getPartialProduct(x: string, y: string): string {
        console.assert(y.length == 1)
        if (x === "0" || y === "0") return "0"
        var result = ""
        var carryOver = 0
        var yNum = Number.parseInt(y)
        for(var i = x.length - 1; i >= 0; i--) {
            let z = Number.parseInt(num1[i]) * yNum + carryOver;
            result = z % 10 + result
            carryOver = Math.floor(z / 10)
        }
        if (carryOver > 0) result = carryOver + result
        return result;        
    }

    function add(x: string, y: string): string {
        // if (x === "0") return y
        // if (y === "0") return x
        
        let maxLength  = Math.max( x.length, y.length)
        x = "0".repeat( maxLength - x.length) + x
        y = "0".repeat( maxLength - y.length) + y
        var result = ""
        var carryOver = 0
        for(var i = maxLength - 1; i >= 0; i--) {
            let z = Number.parseInt(x[i]) + Number.parseInt(y[i]) + carryOver;
            result = z % 10 + result
            carryOver = Math.floor(z / 10)
        }
        if (carryOver > 0) result = carryOver + result
        return result;     
    }   

    var answer = "0"
    var padRight = ""
    for(var i = 0; i < num2.length; i++) {
        let partialProduct = getPartialProduct( num1, num2[ num2.length - (i+1) ])
        if (partialProduct !== "0") partialProduct += '0'.repeat(i)
        answer = add( answer, partialProduct)
    }

    return answer;
};

console.assert( multiply('0', '52') == '0')

console.assert( multiply('20', '10') == '200')
console.assert( multiply('2', '4') == '8')
console.assert( multiply('1451', '42') == '60942')
console.assert( multiply('1930', '0') == '0')
console.log('The END')